﻿using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace Ledict.ConditionExtract.Frm
{
    public partial class AddCaseNode : Form
    {
        CaseCauseBLL causeBLL = new CaseCauseBLL();
        private string parentNodeName = string.Empty;
        private string parentId = string.Empty;
        private string fullPath = string.Empty;
        Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
        Dictionary<string, List<string>> dic1 = new Dictionary<string, List<string>>();
        List<string> table1 = new List<string>();


        public AddCaseNode(string title, string parentName, string nodePath,string parentID)
        {
            InitializeComponent();
            this.Text = title;
            this.txbParentName.Text = parentNodeName = parentName;
            fullPath = nodePath;
            parentId = parentID;
            this.txbParentName.Enabled = false;
            DataTable dt = causeBLL.GetCaseCauseTypeAll();
            if (dt != null && dt.Select(" CC_NodeTypeName in('争议焦点分类','争议焦点','裁判理由','裁判结果','理由要素','理由要素分类')  ").Length > 0)
            {
                DataTable Typedt = dt.Select(" CC_NodeTypeName in('争议焦点分类','争议焦点','裁判理由','裁判结果','理由要素','理由要素分类')  ").CopyToDataTable<DataRow>();
                cbxCCNodeType.DataSource = Typedt;
                cbxCCNodeType.DisplayMember = "CC_NodeTypeName";
                cbxCCNodeType.ValueMember = "CC_NodeType";
            }
            GetReason();
        }

        public AddCaseNode(string title,string ccName, string parentName, string nodePath, string parentID)
        {
            InitializeComponent();
            this.Text = title;
            this.txbCCName.Text = ccName;
            this.txbParentName.Text = parentNodeName = parentName;
            fullPath = nodePath;
            parentId = parentID;
            this.txbParentName.Enabled = false;
            DataTable dt = causeBLL.GetCaseCauseTypeAll();
            if (dt != null && dt.Select(" CC_NodeTypeName in('争议焦点分类','争议焦点','裁判理由','裁判结果','理由要素','理由要素分类')  ").Length > 0)
            {
                DataTable Typedt = dt.Select(" CC_NodeTypeName in('争议焦点分类','争议焦点','裁判理由','裁判结果','理由要素','理由要素分类')  ").CopyToDataTable<DataRow>();
                cbxCCNodeType.DataSource = Typedt;
                cbxCCNodeType.DisplayMember = "CC_NodeTypeName";
                cbxCCNodeType.ValueMember = "CC_NodeType";
            }
            GetReason();
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
            this.Close();
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string name = string.Empty;
            if (txbCCName.Visible == true)
            {
                name = txbCCName.Text.Trim();
            }
            else if (comboBox3.Visible == true)
            {
                name = comboBox3.Text.Trim();
            }
            if (!string.IsNullOrEmpty(name))
            {                
                var model = new SYS_CaseCause();
                NewIDBLL newid = new NewIDBLL();
                model.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                model.CC_Name = name;
                model.CC_ParentID = parentId;
                model.CC_NodeType = Convert.ToInt32(cbxCCNodeType.SelectedValue);
                //if (NodeSubTypeList.Visible=true && NodeSubTypeList.Text.Trim() != "裁判结果")
                //{
                //    model.CC_NodeSubType = Convert.ToInt32(NodeSubTypeList.SelectedValue);
                //}
                model.AllParentId = ElementAnalyzeNew.caseTreeNodeRode.Select(" cc_id='" + parentId + "' ")[0]["AllParentId"].ToString() + "," + model.CC_ID;
                model.AllParentName = ElementAnalyzeNew.caseTreeNodeRode.Select(" cc_id='" + parentId + "' ")[0]["AllParentName"].ToString() + "," + model.CC_Name;
                model.HW_level = model.AllParentName.Split(',').Length;
                model.CC_SerialNumb = causeBLL.GetNodeChildSerialNumber(model.CC_ParentID);
                model.XmlPath = @"/" + fullPath.Replace("\\", @"/") + @"/" + model.CC_Name;
                string ccId = causeBLL.Exists(model.CC_Name, model.CC_ParentID);
                if (!string.IsNullOrEmpty(ccId))
                {
                    MessageBox.Show("已存在相同名称的节点！");
                    return;
                }
                if (causeBLL.AddSYS_CaseCause(model, Gvar.uo.m_displayName))
                {
                    MessageBox.Show("添加成功！");
                }
                //ElementAnalyze.caseTreeNodeRode
                DialogResult = DialogResult.Yes;
            }
            else
            {
                MessageBox.Show("请将节点名称补充完整后再试！");
                txbCCName.Focus();
                return;
            }

        }

        #region 获取XML中某个路径下的节点信息
        /// <summary>
        /// 获取XML中某个路径下的节点信息
        /// </summary>
        /// <param name="path"></param>
        /// <param name="nodePath"></param>
        /// <returns></returns>
        static public XmlNodeList getModeListByPath(string path, string nodePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList nodeList = doc.SelectNodes(nodePath);
            return nodeList;

        }
        #endregion

        #region 裁判理由和裁判结果类别添加
        public void GetReason()
        {
            dic.Clear();
            dic1.Clear();

            string path = System.Environment.CurrentDirectory + @"\表述工具添加子节点.xml";
            XmlNodeList list = getModeListByPath(path, "添加子节点/裁判理由");
            if (list.Count > 0)
            {



                foreach (XmlElement n in list[0].ChildNodes)
                {
                    table1.Add(n.Name);
                    if (n.HasChildNodes)
                    {
                        List<string> names = new List<string>();
                        foreach (XmlElement m in n.ChildNodes)
                        {

                            names.Add(m.Name);
                        }
                        dic.Add(n.Name, names);
                    }
                    else
                    {
                        dic.Add(n.Name, new List<string>());
                    }

                }
            }
            XmlNodeList list1 = getModeListByPath(path, "添加子节点/裁判结果");
            if (list1.Count > 0)
            {
                List<string> list2 = new List<string>();
                foreach (XmlElement item in list1[0].ChildNodes)
                {
                    list2.Add(item.Name);
                }
                dic1.Add("裁判结果", list2);
            }
        }
        #endregion
        private void cbxCCNodeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxCCNodeType.Text.Trim() == "裁判理由")
            {
                if (table1.Count > 0)
                {
                    label39.Visible = true;
                    NodeSubTypeList.Visible = true;
                    NodeSubTypeList.DataSource = table1;
                    comboBox3.Visible = true;
                    txbCCName.Visible = false;
                    NodeSubTypeList.SelectedIndex = 0;
                    if (dic.ContainsKey(NodeSubTypeList.Text))
                    {
                        comboBox3.DataSource = dic[NodeSubTypeList.Text];
                    }
                }
            }
            else if (cbxCCNodeType.Text.Trim() == "裁判结果")
            {
                if (dic1.Count > 0)
                {
                    label39.Visible = false;
                    NodeSubTypeList.Visible = false;
                    //label39.Visible = true;
                    //NodeSubTypeList.Visible = true;
                    //NodeSubTypeList.DataSource = dic1["裁判结果"];
                    comboBox3.Visible = true;
                    txbCCName.Visible = false;
                    if (dic1.ContainsKey("裁判结果"))
                    {
                        comboBox3.DataSource = dic1["裁判结果"];
                    }
                    comboBox3.SelectedIndex = 0;
                }
            }
            else
            {
                label39.Visible = false;
                NodeSubTypeList.Visible = false;
                //NodeSubTypeList.SelectedValue = 0;
                comboBox3.Visible = false;
                txbCCName.Visible = true;
            }
        }

        private void NodeSubTypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(NodeSubTypeList.Text))
            {
                if (dic.ContainsKey(NodeSubTypeList.Text))
                {
                    if (NodeSubTypeList.Text == "其他")
                    {
                        comboBox3.Visible = false;
                        txbCCName.Visible = true;
                    }
                    else
                    {
                        comboBox3.Visible = true;
                        comboBox3.DataSource = dic[NodeSubTypeList.Text];
                        txbCCName.Visible = false;
                    }
                }
            }
        }
    }
}
